"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const kurento_client_1 = __importDefault(require("kurento-client"));
const config_1 = require("../config/config");
const { wsUri } = config_1.lConfig.kurentoServer;
exports.default = (callback) => {
    let client = null;
    return {
        getClient(callback = (error, client) => { }) {
            return (0, kurento_client_1.default)(wsUri, (error, kurentoClient) => {
                if (error) {
                    console.log('Kurento: => ', "Could not find media server at address " + wsUri);
                    return callback("Could not find media server at address" + wsUri
                        + ". Exiting with error " + error);
                }
                client = kurentoClient;
                callback(null, kurentoClient);
                return kurentoClient;
            });
        },
        connect(fromEndpoint, toEndpoint) {
            return fromEndpoint.connect(toEndpoint, (error) => {
                if (error) {
                    console.error('connect: => ', error);
                    return callback && callback(error, client);
                }
                return;
            });
        },
        initEndpoint(client, cb) {
            this.createMediaPipeline(client, (pipeline) => {
                this.createEndpoint(pipeline, (endpoint) => {
                    if (cb)
                        cb(endpoint, pipeline);
                });
            });
        },
        initPlayerEndpoint(client, uri, cb) {
            return this.createMediaPipeline(client, (pipeline) => {
                return this.createPlayerEndpoint(pipeline, uri, (player) => {
                    if (cb)
                        cb(player, pipeline);
                    return { player, pipeline };
                });
            });
        },
        onIceCandidate(webRtcEndpoint, cb) {
            return webRtcEndpoint.on('IceCandidateFound', (event) => {
                var iceCandidate = kurento_client_1.default.getComplexType('IceCandidate')(event.candidate);
                cb && cb(iceCandidate);
                return iceCandidate;
            });
        },
        createMediaPipeline(client, cb) {
            return client.create('MediaPipeline', (error, pipeline) => {
                if (error) {
                    console.error('createMediaPipeline: => ', error);
                    return callback && callback(error, client);
                }
                cb && cb(pipeline);
                return pipeline;
            });
        },
        createEndpoint(pipeline, cb) {
            return pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
                if (error) {
                    console.error('createEndpoint: => ', error);
                    return callback && callback(error, client);
                }
                cb && cb(webRtcEndpoint);
                return webRtcEndpoint;
            });
        },
        createPlayerEndpoint(pipeline, uri, cb) {
            return pipeline.create('PlayerEndpoint', { uri }, (error, player) => {
                if (error) {
                    console.error('PlayerEndpoint: => ', error);
                    return callback && callback(error, client);
                }
                cb && cb(player);
                return player;
            });
        },
        createRecordEndpoint(pipeline, uri, cb) {
            return pipeline.create('RecorderEndpoint', { uri }, (error, recorder) => {
                if (error) {
                    console.error('RecorderEndpoint: => ', error);
                    return callback && callback(error, client);
                }
                cb && cb(recorder);
                return recorder;
            });
        },
        processOffer(endpoint, sdpOffer, cb) {
            endpoint.processOffer(sdpOffer, (error, sdpAnswer) => {
                if (error) {
                    console.error('processOffer: => ', error);
                    return callback && callback(error, client);
                }
                cb && cb(sdpAnswer);
            });
            endpoint.gatherCandidates((error) => {
                if (error) {
                    return callback && callback(error, client);
                }
            });
        },
        stop(members, socket, userId) {
        }
    };
};
